package com.pan.show.record.impl;

import com.pan.show.record.Record;
import com.pan.utils.ApplicationConfig;

import javax.swing.*;
import java.util.Arrays;

public class FrameRecord implements Record<String> {


    private JFrame frame;
    public FrameRecord() {
        frame = new JFrame("Record");
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }

    @Override
    public void setContent(String content) {
        String[] lines = content.split("\n");
        int rows = lines.length;
        int cols = Arrays.stream(lines).map(String::length).sorted((a,b)->b-a).findFirst().get();

        JTextArea textArea = new JTextArea(rows,cols);
        textArea.setFont(ApplicationConfig.INSTANCE.getFont());
        textArea.setText(content);
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);
        frame.add(scrollPane);
        frame.pack();
    }

    @Override
    public void display() {
        frame.setVisible(true);
    }

    @Override
    public void close() {
    }

    public static void main(String[] args) {
        FrameRecord frameRecord = new FrameRecord();
        frameRecord.setContent("1145141111111111111111111111111\n223\n114");
        frameRecord.display();
        frameRecord.close();
    }
}

